<template>
  <div class="comment">
    <div class="addcomment"
         v-show='!isFocus'>
      <input type="text"
             @focus="showCommentBox"
             placeholder="写跟帖" />
      <span class="comment"
            @click="toCommentList">
        <i class="iconfont iconpinglun-"></i>
        <em>{{obj.comment_length}}</em>
      </span>
      <i class="iconfont iconshoucang "
         @click="starFn(obj.id)"
         :class="{Iconshoucang:obj.has_star}"></i>
      <i class="iconfont iconfenxiang"></i>
    </div>
    <div class="inputcomment"
         v-show='isFocus'>
      <textarea ref='commtext'
                rows="5"
                v-model="content"></textarea>
      <div>
        <span @click="sendFn">发 送</span>
        <span @click="cancelFn">取 消</span>
      </div>
    </div>
  </div>
</template>

<script>
import { publisher, post_star } from '@/api/news'
export default {
  props: {
    obj: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      isFocus: false,
      content: '', //回复内容
      parent_id: '', //回复id
    }
  },
  methods: {
    cancelFn() {
      this.content = ''
      this.isFocus = false
      this.parent_id = ''
    },
    toCommentList() {
      this.$router.push({ path: '/commentList' })
    },
    // 显示发表评论容器
    showCommentBox() {
      this.isFocus = true
      //   让多行文本域自动聚焦
      // 如何获取DOM？答：用 ref属性
      // 此刻获取不到最新的文本域，因此通过下面的面试设置聚焦失败
      // this.$refs.commtext.focus()

      // 为什么获取不到呢？因为DOM更新是异步的
      // 如何解决？答：可以通过 $nextTick()方法来获取到最新DOM
      // 因此在 $nextTick()方法的回调中就能够获取更新后的DOM
      this.$nextTick(() => this.$refs.commtext.focus())
    },
    // 发表评论
    sendFn() {
      let obj = { content: this.content }
      if (this.parent_id) {
        obj.parent_id = this.parent_id
      }
      publisher(this.obj.id, obj).then((res) => {
        if (res.data.message === '评论发布成功') {
          this.$toast.success(res.data.message)
          //   通知父组件刷新评论列表
          this.$emit('updateComment')
          // 清空发布框内容
          this.content = ''
          // 隐藏多行文本域
          this.isFocus = false
          //   清空回复id
          this.parent_id = ''
        }
      })
    },
    // 是否收藏
    starFn(id) {
      post_star(id).then((res) => {
        if (res.data.message === '收藏成功') {
          this.obj.has_star = true
          this.$toast.success(res.data.message)
        } else {
          this.obj.has_star = false
          this.$toast.success(res.data.message)
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.Iconshoucang {
  color: red;
}
.comment {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
}
.inputcomment {
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  display: flex;
  background-color: #fff;
  align-items: flex-end;
  textarea {
    flex: 3;
    background-color: #eee;
    border: none;
    border-radius: 10px;
    padding: 10px;
  }
  div {
    padding: 20px;
  }
  span {
    display: block;
    flex: 1;
    height: 24px;
    line-height: 24px;
    padding: 0 10px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    font-size: 13px;
  }
}
.addcomment {
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  margin-top: 20px;
  display: flex;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  background-color: #fff;
  > input {
    flex: 4;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #eee;
    padding-left: 20px;
    font-size: 14px;
  }
  i {
    font-size: 20px;
  }
  > span {
    flex: 1;
    position: relative;
    > em {
      position: absolute;
      right: 0;
      top: -5px;
      font-size: 10px;
      background-color: #f00;
      color: #fff;
      border-radius: 5px;
      padding: 3px 5px;
    }
  }
  > i {
    flex: 1;
  }
}
</style>